#include "LockManager.h"
#include "Lock_ICDLL.h"
#include "Lock_V9RFDLL.h"
#include "Lock_TPDLL.h"
#include "Lock_RFV42.h"
#include "Lock_proUsb.h"
#include "Card_CVR100UDDLL.h"
#include "DLLWrapper.h"

LockManager *LockManager::instance()
{
	static LockManager inst;
	return &inst;
}

LockManager::LockManager()
{
	QMap<QString,AbstractLock*>::Iterator it = m_locks.begin();
	while(it != m_locks.end())
	{
		delete it.value();
		it++;
	}
}

LockManager::~LockManager()
{
}

void LockManager::initLock()
{
	//ICDLL
	{
		Lock_ICDLL *lockIC = new Lock_ICDLL();	
		if(lockIC->isVaild())
			m_locks.insert("ICDLL",lockIC);
		else
			delete lockIC;
	}

	//V9RF
	{
		Lock_V9RFDLL *lockV9 = new Lock_V9RFDLL();
		if(lockV9->isVaild())
			m_locks.insert("V9RFDLL",lockV9);
		else
			delete lockV9;
	}

	//TP
	{
		Lock_TPDLL *lockTp = new Lock_TPDLL();
		if(lockTp->isVaild())
			m_locks.insert("TPDLL",lockTp);
		else
			delete lockTp;
	}

	//TP
	{
		Lock_RFV42 *lockV42 = new Lock_RFV42();
		if(lockV42->isVaild())
			m_locks.insert("RFV42DLL",lockV42);
		else
			delete lockV42;
	}

	//proUsb
	{
		Lock_proUsb *lockProUsb = new Lock_proUsb();
		if(lockProUsb->isVaild())
			m_locks.insert("ProUsb",lockProUsb);
		else
			delete lockProUsb;
	}
}

void LockManager::initCard()
{
	//CVR100UDDLL
	{
		Card_CVR100UDDLL *lockCVR100UD = new Card_CVR100UDDLL();
		if(lockCVR100UD->isVaild()) //->init("CVR100UDDLL"))
			m_cards.insert("CVR100UDDLL",lockCVR100UD);
		else
			delete lockCVR100UD;
	}
}

QString LockManager::LockString(int lockManufactuer)
{
	if(lockManufactuer == 1)
		return "ICDLL";
	else if(lockManufactuer == 2)
		return "V9RFDLL";
	else if(lockManufactuer == 3)
		return "TPDLL";
	else if(lockManufactuer == 4)
		return "RFV42DLL";
	else if(lockManufactuer == 5)
		return "ProUsb";

	return QString();
}

QString LockManager::CardString(int lockManufactuer)
{
	if(lockManufactuer == 1)
		return "CVR100UDDLL";

	return QString();
}

AbstractLock *LockManager::GetLock(QString name)
{
	if(m_locks.contains(name))
		return m_locks[name];

	return NULL;
}

int LockManager::GetLockCount()
{
	return m_locks.count();
}

AbstractLock *LockManager::GetCard(QString name)
{
	if(m_cards.contains(name))
		return m_cards[name];

	return NULL;
}

int LockManager::GetCardCount()
{
	return m_cards.count();
}

Json::Value LockManager::DealCommand(int type,QString cmd,Json::Value &param)
{
	if(type == 0)
	{
		int lockType = param["lockManufactuer"].asInt();		
		auto lock = GetLock(LockString(lockType));
		if(lock)
			return lock->DealCommand(cmd,param);
	}
	else if(type == 1)
	{
		int cardType = param["cardManufactuer"].asInt();
		auto card = GetCard(CardString(cardType));
		if(card)
			return card->DealCommand(cmd,param);
	}

	Json::Value ret;
	ret["code"] = -1;
	ret["message"] = "no such plugin";
	return ret;
}

Json::Value LockManager::DealCommandThread(int type,QString cmd,Json::Value &param)
{
	if(type == 0)
	{
		Json::Value lock = param.get("lockManufactuer",Json::Value());
		if(lock.isNull() || !lock.isInt())
		{
			Json::Value err;
			err["code"] = -1;
			err["message"] = "invaild lockManufactuer";
			return err;
		}
	}
	else if(type == 1)
	{
		Json::Value lock = param.get("cardManufactuer",Json::Value());
		if(lock.isNull() || !lock.isInt())
		{
			Json::Value err;
			err["code"] = -1;
			err["message"] = "invaild cardManufactuer";
			return err;
		}
	}
	
	Json::Value ret;
	emit sigCommand(type,cmd,&param,&ret);
	return ret;
}